<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2017/4/3
 * Time: 上午9:09
 * 设置
 */
class Setting extends MY_Controller
{
	public $left_menu_min;

	function __construct(){
		parent::__construct();
		$parent_id = 12;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "基础设置", "curr" => '/setting', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", 'setting');
	}

	function index(){
		$school_info = get_field_by_where("o2o_school", "*", "id='{$this->user_data['school_id']}'");
		if(!$school_info){
			show_error('登录失效');exit;
		}
		$post_data = $this->input->post();
		if($post_data){
			if(!empty($post_data['school_logo']) && !strstr($post_data['school_logo'], 'http://') ){
				$post_data['school_logo'] = get_qiniu_url($post_data['school_logo']);
			}
			if(!empty($post_data['wx_share_logo']) && !strstr($post_data['wx_share_logo'], 'http://') ){
				$post_data['wx_share_logo'] = get_qiniu_url($post_data['wx_share_logo']);
			}
			if(!empty($post_data['wx_mp_qrcode']) && !strstr($post_data['wx_mp_qrcode'], 'http://') ){
				$post_data['wx_mp_qrcode'] = get_qiniu_url($post_data['wx_mp_qrcode']);
			}
			if(!empty($post_data['wx_manager_qrcode']) && !strstr($post_data['wx_manager_qrcode'], 'http://') ){
				$post_data['wx_manager_qrcode'] = get_qiniu_url($post_data['wx_manager_qrcode']);
			}
			if(!empty($post_data['grow_up'])){
				$post_data['grow_up'] = get_qiniu_url($post_data['grow_up']);
			}
			if(!empty($post_data['service'])){
				$post_data['service'] = get_qiniu_url($post_data['service']);
			}

			if(!empty($post_data['lgn_lat'])){
				$lgn_lat = explode(",", $post_data['lgn_lat']);
				$post_data['longitude'] = $lgn_lat[0];
				$post_data['latitude'] = $lgn_lat[1];
				unset($post_data['lgn_lat']);
			}
			$this->db->where("id", $this->user_data['school_id']);
			$this->db->update("o2o_school", $post_data);
			show_message($this->jump_url);
		}else{
			$this->load->model("Areas");
			$province_list = $this->Areas->get_province_data();
			if($school_info['province_code'] > 0){
				$city_list = $this->Areas->get_province_data($school_info['province_code']);
				$this->smarty->assign("city_list", $city_list);
			}

			if($school_info['city_code'] > 0){
				$area_list = $this->Areas->get_province_data($school_info['city_code']);
				$this->smarty->assign("area_list", $area_list);
			}
			$this->smarty->assign("province_list", $province_list);
			$this->smarty->assign("info", $school_info);
			$this->smarty->display("setting/info.html");
		}
	}

	function base_info(){
		$this->config->load('ws_config');
		$ws_config = $this->config->item('ws_url_config');
		$this->load->model('Teachers_model');
		$info = $this->Teachers_model->get_teacher_by_user_id($this->user_data['id']);
		$this->smarty->assign("info", $info);
		if($info){
			$post_data = $this->input->post();
			if($post_data){
				if(!empty($post_data['school_logo']) && !strstr($post_data['school_logo'], 'http://') ){
					$post_data['school_logo'] = get_qiniu_url($post_data['school_logo']);
				}
				$this->Teachers_model->update_info_by_self($info['id'],$post_data);
				header("Location:/setting/base_info");exit;
			}
		}
		$this->smarty->assign("ws_config", $ws_config);


		$this->smarty->display("setting/base_info.html");
	}

}
